﻿using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Skyhook.Core;
using Skyhook.Resources;

namespace Skyhook.Sprites
{
    public class SpriteEngine : IVisualsEngine
    {
        SpriteFactory Factory = null;
        SpriteRenderComponent RenderComponent = null;

        Microsoft.Xna.Framework.Game Game = null;

        public SpriteEngine()
        {
            
        }

        public void InitializeAndRegisterComponent(Microsoft.Xna.Framework.Game game, IOnScreenView view, int draworder)
        {
            Game = game;
            SpriteProjection.SetViewSize(game.GraphicsDevice.Viewport.Width, game.GraphicsDevice.Viewport.Height);
            RenderComponent = new SpriteRenderComponent(Game, new SpriteWorldVisualizer(view, game));
            Game.Components.Add(RenderComponent);
            RenderComponent.DrawOrder = draworder;
            Factory = new SpriteFactory();
        }

        public IVisualObject CreateVisualObject(string spritename,IResource image, int[] groupnums, string layername, IRenderer renderer = null)
        {
            ImageCollection imageconverted = image as ImageCollection;
            if (imageconverted == null)
            {
                throw new Exception("Incorrect resource class " + image.GetType().Name + "!");
            }
            return Factory.CreateSprite(spritename, imageconverted, groupnums, layername, renderer);
        }

        public IRenderer CreateVisualRenderer(int type)
        {
            return new SpriteCenterDividedRenderer(type);
        }

        public int VisualsOnScreen
        {
            get
            {
                return RenderComponent.SpritesOnScreen;
            }
        }
    }
}
